#coding=utf-8
import requests,json,urllib
import ID_card
import admin_login_api
import community_purchase_apply
def submit(mobile,purchaserName,auditStatus,productId):
    idcard = str(ID_card.idcard())
    #定义headers
    headers = {'Accept': 'application/json, text/plain, */*', 'Accept-Encoding': 'gzip, deflate','Accept-Language': 'zh-CN,zh;q=0.9'
                ,'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1','X-Requested-With': 'XMLHttpRequest'
                , 'Referer': 'http://community-platform-api-test.gdgyun.com/pages/purchaseApply.html', 'Origin': 'http://community-platform-api-test.gdgyun.com', 'Host': 'community-platform-api-test.gdgyun.com'
                , 'Content-Type': 'application/json;charset=UTF-8','Authorization': 'Bearer Test321'}
    s = requests.session()
    if productId == '1':
        level = 'ONE'
    if productId == '2':
        level = 'TWO'
    if productId == '3':
        level = 'THREE'
    # if productId == '6':
    #     level = 'FOUR'
    if productId == '4':
        level = 'UPGRADE_PACKAGE_LEVEL_1'
    if productId == '5':
        level = 'UPGRADE_PACKAGE_LEVEL_2'
    if productId == '6':
        level = 'UPGRADE_PACKAGE_LEVEL_3'
    data = ({"salesmanMobile":"13438825857","purchaserMobile":str(mobile),"purchaserName":str(purchaserName)
        ,"paymentVouchers":["http://community-fs-api-test.gdgyun.com/uploads/temp/images/8d55b231f0d1c750676bf3bcc7dd5bfa67b7fab0.jpg"
            ,"http://community-fs-api-test.gdgyun.com/uploads/temp/images/e3a59824cbc2a94abb06e9bf492c4ecf70b50396.jpg"
            ,"http://community-fs-api-test.gdgyun.com/uploads/temp/images/fbb5175715e887f6f174955d43059c3551a291b8.jpg"]
        ,"purchaserIdCardNo":idcard,"paymentWay":"WECHAT","level":str(level)})

    submit = s.post('http://community-platform-api-test.gdgyun.com/purchaseApply/submit',json=data,headers=headers)   #提交购买申请
    msg = str(submit.json()["msg"])
    if msg == '提交云社区购买申请成功':
        #进入后台管理系统进行审核操作
        headers1 = admin_login_api.admin_login('17700010005')  # 接收17781108472登录后的headers
        list_index = s.get('http://community-bms-api-test.gdgyun.com/mgt/api/community/purchaseApply/list',headers=headers1)
        id = list_index.json()["data"]["list"][0]["id"]
        examine_data = ({"auditStatus":str(auditStatus),"auditRemark":"大大大所多撒","productId":int(productId),"id":id})    #auditStatus状态：PASSED为通过，REJECTED为驳回；productId：1/2/3分别为社区的等级：1/2/3
        examine = s.post('http://community-bms-api-test.gdgyun.com/mgt/api/community/purchaseApply/audit/submit',json=examine_data,headers=headers1)   #进行审核操作
        examine_msg = examine.json()["msg"]
        if examine_msg == '云社区购买申请审核成功，通知短信已发往用户手机':
            #审核通过后，前往数据库查询验证码
            code = community_purchase_apply.code(mobile)
            print u'用户的购买申请码为：'+code
            return u'用户的购买申请码为：'+code,code

        else:
            return examine_msg,id
    else:
        code = community_purchase_apply.code(mobile)
        print u'手机号为【'+str(mobile)+u'】的用户已经购买一个云社区了，不能重复进行购买已经存在'
        return str(msg)+ u'，付款码为：' +str(code),'fail'




if __name__ =='__main__':
    submit('13800010048','李哈哈','PASSED','2')

